package com.mingtai.design.pattern.creationalming.singleton;

import java.io.Serializable;

/**
 * @author: tianmingtai
 * @create: 2021/01/07 20:18
 * @description: 饿汉式单例 此方式比较常用
 * 优点：没有加锁，执行效率会提高。
 * 缺点：类加载时就初始化，浪费内存。
 */

public class HungrySingleton implements Serializable {

    private final static HungrySingleton hungrySingleton;

    static {
        hungrySingleton = new HungrySingleton();
    }

    /**
     * 私有化构造器
     */
    private HungrySingleton() {
        if (hungrySingleton != null) {
            throw new RuntimeException("单例构造器禁止反射调用");
        }
    }

    public static HungrySingleton getInstance() {
        return hungrySingleton;
    }


}
